package com.cjk.console.ad;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cjk.console.model.ad.AdRootJson;
import com.cjk.console.model.ad.AdUpdateInfo;
import com.cjk.foundation.entity.file.FileModuleEntity;
import com.cjk.foundation.util.FileUtil;

public class AdMng {
	
//	private final static Logger LOGGER = LoggerFactory.getLogger(AdMng.class);
	
	public static final String DEFAULT_VERSION = "0.0.1";

	public static String buildVersion(String version) {
		if (StringUtils.isEmpty(version)) {
			return DEFAULT_VERSION;
		}
		Integer[] versionNum = new Integer[3];
		String[] versionTmps = version.split("\\.");
		for (int i = 0; i < 3; i++) {
			versionNum[i] = Integer.valueOf(versionTmps[i]);
		}
		Integer first = versionNum[2] + 1;
		Integer second = versionNum[1];
		Integer third = versionNum[0];
		if (first > 999) {
			second += 1;
			first = 0;
			if (second > 999) {
				third += 1;
				second = 0;
			}
		}
		version = third + "." + second + "." + first;
		return version;
	}
	
	public static Integer buildUpdateTimestamp() {
		String timestamp = String.valueOf(new Date().getTime());
		int length = timestamp.length();
		return Integer.valueOf(timestamp.substring(0, length - 3));
	}
	
	public static String buildDirPath(String baseDir, Integer groupId, Integer userId) {
		return baseDir.replace("{userId}", String.valueOf(userId)).replace("{groupId}", String.valueOf(groupId));
	}
	
	public static void makeRootJson(String sn) {
		AdRootJson adRoot = new AdRootJson();
		adRoot.setServerSN(sn);
		adRoot.setPlaylistUrl("http://192.168.254.1/vakuanlivetv.html");
		adRoot.setEtvAppDesUrl("http://192.168.254.1/etv/hotel_update.json");
		FileUtil.writeFile("/easyTv/data/" + sn + "/", "root.json", JSON.toJSONString(adRoot));
	}
	
	public static void makeHotelUpdateJson(String dir, List<FileModuleEntity> fileModuleEntities) {
		JSONObject object = new JSONObject();
		for (FileModuleEntity fileModuleEntity : fileModuleEntities) {
			AdUpdateInfo adUpdateInfo = new AdUpdateInfo();
			String version = StringUtils.isEmpty(fileModuleEntity.getVersion()) ? "0.0.1" : fileModuleEntity.getVersion();
			adUpdateInfo.setVersionNo(version);
			adUpdateInfo.setUpdateVersionInfo(fileModuleEntity.getUpdateInfo());
			adUpdateInfo.setIsNeedUpdateNow(fileModuleEntity.getNeedUpdate().getCode());
			Integer updateTimestamp = fileModuleEntity.getUpdateTimestamp() == null ? 0 : fileModuleEntity.getUpdateTimestamp();
			adUpdateInfo.setUpdateTimestamp(updateTimestamp);
			adUpdateInfo.setDownloadURL(fileModuleEntity.getDownloadUrl());
			adUpdateInfo.setType(fileModuleEntity.getModule());
			object.put(fileModuleEntity.getModule(), adUpdateInfo);
		}
		FileUtil.writeFile(dir, "hotel_update.json", object.toJSONString());
	}
	
	public static void makeHotelServiceJson(String dir, String content) {
		FileUtil.writeFile(dir, "hotel_service.json", content);
	}
	
}
